<template>
  <div class="container">
    <div class="user-info">
      <img class="avatar" :src="msgList.avatar" alt="" />
      <div class="username">{{ msgList.nickname }}</div>
      <div class="sign">{{ msgList.signature }}</div>
      <div class="info">
        <div class="info-cell">
          <span>{{ msgList.fans }} </span>粉丝
        </div>
        <div class="info-cell">
          <span>{{ msgList.concerns }} </span>关注
        </div>
        <div class="info-cell">
          <span>{{ msgList.dynamics }} </span>视频
        </div>
      </div>
      <div class="info-btn">
        <!-- <button class="share-btn">分享</button> -->
        <button @click="attention($event)" class="follow-btn" :style="{background: mainColor}">
          <template v-if="this.msgList.is_link == 1">
            取消关注
          </template>
          <template v-else>
            +关注
          </template>
        </button>
      </div>
    </div>
    <div :class="{ 'tab-fixed': tabFixed, nav: true }">
      <div
        @click="currentNav = 1"
        :class="{ 'nav-cell': true, active: currentNav === 1 }"
        :style="{borderBottomColor: mainColor}"
      >
        全部
      </div>
    </div>
    <div
      class="dynamic_wrap"
      v-if="currentNav === 1"
      :style="{ paddingTop: tabFixed ? 1.8 + 'rem' : 0 }"
    >
      <div class="dynamic">
        <div class="col col-left">
          <div
            :class="{ 'dynamics-cell': true, long: (index + 1) % 2 === 0 }"
            v-for="(item, index) in videoListL"
            :key="index"
          >
            <div @click="tabClick(item.id, item.is_link)" class="video">
              <template v-if="item.type == 1">
                <img :src="item.links" style="width:100%" />
              </template>
              <template v-if="item.type == 2">
                <img :src="item.cover" />
              </template>

              <div class="play"></div>
              <div class="follow" :style="{background: mainColor}">你的关注</div>
            </div>
            <div class="info">
              <div class="info-text">
                {{ item.describe }}
              </div>
              <div class="sub-text">
                <div class="left">
                  <img :src="item.avatar" alt="" />
                  {{ item.nickname }}
                </div>
                <div class="right">
                  <img
                    @click="like(item.id, $event)"
                    src="@/assets/用户主页2-assets/icon3.png"
                    alt=""
                  />
                  <span class="zan">{{ item.like }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="col col-right">
          <div
            :class="{ 'dynamics-cell': true, long: (index + 2) % 2 === 0 }"
            v-for="(item, index) in videoListR"
            :key="index"
          >
            <div @click="tabClick(item.id, item.is_link)" class="video">
              <template v-if="item.type == 1">
                <img :src="item.links[0]" style="width:100%" />
              </template>
              <template v-if="item.type == 2">
                <img :src="item.cover" />
              </template>
              <div class="play"></div>
              <div class="follow" :style="{background: mainColor}">你的关注</div>
            </div>
            <div class="info">
              <div class="info-text">
                {{ item.describe }}
              </div>
              <div class="sub-text">
                <div class="left">
                  <img :src="item.avatar" alt="" />
                  {{ item.username }}
                </div>
                <div class="right">
                  <img
                    @click="like(item.id, $event)"
                    src="@/assets/用户主页2-assets/icon3.png"
                    alt=""
                  />
                  <span class="zan">{{ item.like }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <loadMore @loadMore="loadMore"></loadMore>
        <!-- <dynamic-panel1 v-if="currentNav === 2 && dynamicList.lenght" /> -->
      </div>
    </div>
  </div>
</template>

<script>
import loadMore from "@/components/load-more";
import {
  getUserHomePage,
  getHomeDynamic,
  sendZan,
  follow
} from "@/api/dynamics";
import { mapState } from 'vuex'
export default {
  data() {
    return {
      currentNav: 1,
      dynamicStatus: 1,
      tabFixed: false,
      tabScrollTop: null,
      msgList: {},
      videoListL: [],
      videoListR: [],
      isLink: "",
      page: 1,
      isAll: false,
      loading: false,
      dynamicList: []
    };
  },
  computed: {
    ...mapState({
      mainColor: state => state.app.mainColor,
      subColor: state => state.app.subColor
    })
  },
  methods: {
    loadMore() {
      if (this.loading || this.isAll) return;
      this.loading = true;
      this.getDynamic(++this.page);
    },
    handleScroll() {
      let scrollTop =
        document.documentElement.scrollTop ||
        window.pageYOffset ||
        document.body.scrollTop;
      this.tabFixed = scrollTop >= this.tabScrollTop ? true : false;
    },
    like(dynamicsId, $event) {
      sendZan({
        id: dynamicsId
      }).then(res => {
        let state = res.data.data.type;
        let zan = $event.target.nextSibling;
        let zanNum = zan.innerText;
        if (state == 1) {
          zanNum++;
          zan.innerText = zanNum;
        }
        if (state == 2) {
          zanNum--;
          zan.innerText = zanNum;
        }
      });
    },
    getDynamic(page) {
      getHomeDynamic({
        user: this.$route.query.user_id,
        type: "",
        page
      }).then(res => {
        this.dynamicList = res.data.data;
        if (page === 1) {
          this.videoListL = [];
          this.videoListR = [];
        }
        for (let i = 0; i < this.dynamicList.length; i++) {
          if (i % 2 == 0) {
            this.videoListL.push(this.dynamicList[i]);
          } else {
            this.videoListR.push(this.dynamicList[i]);
          }
        }
      });
    },
    attention(e) {
      follow({
        user: this.$route.query.user_id
      }).then(res => {
        //关注成功
        // if (this.isLink != null) {
        if (res.data.data.type == 1) {
          e.target.innerText = "取消关注";
          // sessionStorage.setItem("isLink", 1);
        }
        //取消关注成功
        if (res.data.data.type == 2) {
          e.target.innerHTML = "+关注";
          // sessionStorage.setItem("isLink", 0);
        }
        // } else {
        // this.$router.push("/main-login");
        // }
      });
    },
    tabClick(id, isLink) {
      this.$router.push({
        path: "/dynamics-detail",
        query: { id: id, is_link: isLink }
      });
    }
  },
  components: {
    loadMore,
  },
  mounted() {
    this.tabScrollTop = document.getElementsByClassName("nav")[0].offsetTop;
    window.addEventListener("scroll", this.handleScroll, true);
    //获取用户主页信息
    getUserHomePage({
      user: this.$route.query.user_id
    }).then(res => {
      this.msgList = res.data.data;
    });
    //获取用户主页-单个用户获取
    this.getDynamic(1);
    //获取到isLink
    this.isLink = sessionStorage.getItem("isLink");
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  }
};
</script>

<style lang="less" scoped>
.container {
  height: auto;

  .user-info {
    background: #fff;
    position: relative;
    padding: 0 4.2%;
    margin-bottom: 0.48rem;

    .avatar {
      height: 2.8rem;
      width: 2.8rem;
      margin: 0.8rem 0;
    }

    .username {
      color: rgba(68, 68, 68, 1);
      font-size: 0.8rem;
      font-weight: bold;
      line-height: 0.8rem;
    }

    .sign {
      color: rgba(68, 68, 68, 1);
      font-size: 0.52rem;
      margin-top: 0.3rem;
    }

    .info {
      display: flex;
      padding: 1.2rem 0 0.8rem;

      .info-cell {
        color: rgba(68, 68, 68, 0.6);
        font-size: 0.48rem;
        margin-right: 0.7rem;

        span {
          font-size: 0.64rem;
          font-weight: bold;
          color: rgba(68, 68, 68, 1);
        }
      }
    }

    .info-btn {
      position: absolute;
      right: 0.52rem;
      top: 1.6rem;

      button {
        border: none;
        height: 1.2rem;
        line-height: 1rem;
        font-size: 0.48rem;
        padding: 0 0.4rem;
        border-radius: 0.12rem;
        margin-left: 0.36rem;
      }

      .share-btn {
        color: rgba(68, 68, 68, 1);
        border: 1px solid rgba(233, 233, 233, 1);
        background: none;
      }

      .follow-btn {
        background: rgba(0, 120, 255, 1);
        color: #fff;

        img {
          height: 0.3rem;
          width: 0.3rem;
          vertical-align: middle;
          margin: 0;
        }
      }
    }
  }

  .nav {
    background: #fff;
    display: flex;
    height: 1.8rem;
    line-height: 1.8rem;
    color: rgba(68, 68, 68, 1);
    font-size: 0.64rem;
    padding: 0 4.2%;

    .nav-cell {
      height: 1.4rem;
      margin-right: 1.4rem;
      transition-duration: 0.2s;
    }

    .active {
      font-weight: bold;
      font-size: 0.8rem;
      color: rgba(51, 51, 51, 1);
      border-bottom: 0.24rem solid rgba(0, 120, 255, 1);
    }
  }

  .tab-fixed {
    position: fixed !important;
    width: 100%;
    left: 0;
    top: 0;
    z-index: 999;
  }

  .dynamic_wrap {
    img {
      width: 100%;
    }

    .dynamic {
      padding: 0.48rem;
      display: flex;
      justify-content: space-between;

      .col {
        width: 49%;

        .dynamics-cell {
          background: #fff;
          border-radius: 0.12rem;
          overflow: hidden;
          margin-bottom: 0.2rem;

          .video {
            //height: 9.28rem;
            background-color: #dfdfdf;
            position: relative;

            .play {
              position: absolute;
              height: 1.12rem;
              width: 1.12rem;
              background-image: url(".././../assets/用户主页2-assets/icon1.png");
              background-size: 100% 100%;
              top: 0.36rem;
              right: 0.36rem;
            }

            .video img {
              width: 100% !important;
            }

            .follow {
              position: absolute;
              bottom: 0.36rem;
              left: 0.36rem;
              color: #fff;
              font-size: 0.36rem;
              background: rgba(0, 120, 255, 1);
              width: 1.82rem;
              height: 0.68rem;
              text-align: center;
              line-height: 0.68rem;
              border-radius: 0.12rem;
            }
          }

          .info {
            padding: 0.4rem 0.36rem;

            .info-text {
              font-size: 0.52rem;
              font-family: "PingFang-SC-Bold";
              font-weight: bold;
              color: rgba(68, 68, 68, 1);
              height: 1.4rem;
              overflow: hidden;
            }

            .sub-text {
              display: flex;
              justify-content: space-between;
              color: #444444;
              margin-top: 0.32rem;

              .left {
                font-size: 0.52rem;

                img {
                  width: 0.72rem;
                  height: 0.72rem;
                  vertical-align: middle;
                  border-radius: 50%;
                }
              }

              .right {
                font-size: 0.48rem;

                img {
                  width: 0.56rem;
                  vertical-align: middle;
                }

                .zan {
                  margin: 0 0.1rem;
                }
              }
            }
          }
        }

        // .long {
        //     .video {
        //         //height: 11.06rem;
        //     }
        // }
      }
    }
  }
}
</style>
